using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace F1RacingGame
{
    public class Car
    {
        // 赛车属性
        public string Name { get; set; }
        public float MaxSpeed { get; set; }
        public float NormalSpeed { get; set; } // 正常速度（不加速时的速度）
        public float Acceleration { get; set; }
        public float Handling { get; set; }
        public float Durability { get; set; }
        
        // 油量系统
        public float Fuel { get; set; } = 100.0f; // 初始油量100秒
        public float MaxFuel { get; set; } = 100.0f; // 最大油量
        public bool IsFuelEmpty => Fuel <= 0; // 油量是否耗尽
        
        // 当前位置和状态
        public float X { get; set; }
        public float Y { get; set; }
        public float Speed { get; set; }
        public float Direction { get; set; } // 角度，0度为向右，90度为向下
        public float RotationAngle { get; set; } = 0; // 旋转角度
        public bool IsAccelerating { get; set; }
        public bool IsBraking { get; set; }
        public bool IsTurningLeft { get; set; }
        public bool IsTurningRight { get; set; }
        
        // 物理属性
        public float Friction { get; set; } = 0.98f;
        public float TurnSpeed { get; set; } = 3.0f;
        public float MaxTurnSpeed { get; set; } = 5.0f;
        
        // 尺寸 - 调整为像素风格
        public int Width { get; set; } = 16;  // 8位机像素风格
        public int Height { get; set; } = 24; // 保持比例
        
        public Car(string name, float maxSpeed, float acceleration, float handling, float durability)
        {
            Name = name;
            MaxSpeed = maxSpeed;
            NormalSpeed = maxSpeed * 0.7f; // 正常速度为最大速度的70%
            Acceleration = acceleration;
            Handling = handling;
            Durability = durability;
            
            // 根据操控性调整转向速度
            TurnSpeed = handling / 20.0f;
            MaxTurnSpeed = handling / 10.0f;
            
            // 初始化油量
            Fuel = MaxFuel;
        }
        
        // 消耗油量
        public void ConsumeFuel(float amount)
        {
            Fuel = Math.Max(0, Fuel - amount);
        }
        
        // 增加油量
        public void AddFuel(float amount)
        {
            Fuel = Math.Min(MaxFuel, Fuel + amount);
        }
        
        // 重置油量
        public void ResetFuel()
        {
            Fuel = MaxFuel;
        }
        
        public void Update()
        {
            // 处理加速
            if (IsAccelerating && Speed < MaxSpeed)
            {
                Speed += Acceleration * 0.1f;
                if (Speed > MaxSpeed)
                    Speed = MaxSpeed;
            }
            else if (!IsAccelerating && Speed > NormalSpeed)
            {
                // 不加速时恢复到正常速度
                Speed -= Acceleration * 0.05f;
                if (Speed < NormalSpeed)
                    Speed = NormalSpeed;
            }
            
            // 处理刹车
            if (IsBraking)
            {
                Speed *= 0.9f;
            }
            
            // 处理转向 - 在上下卷轴游戏中，左右转向改变X位置
            if (IsTurningLeft)
            {
                float turnAmount = TurnSpeed * (Speed / MaxSpeed);
                X -= turnAmount;
            }
            if (IsTurningRight)
            {
                float turnAmount = TurnSpeed * (Speed / MaxSpeed);
                X += turnAmount;
            }
            
            // 应用摩擦力
            Speed *= Friction;
            
            // 更新位置 - 在上下卷轴游戏中，车辆向下移动
            Y += Speed;
            
            // 边界检查 - 只检查左右边界，上下边界由游戏逻辑处理
            if (X < Width / 2) X = Width / 2;
            if (X > GameEngine.GAME_WIDTH - Width / 2) X = GameEngine.GAME_WIDTH - Width / 2;
        }
        
        // 检查是否碰到赛道边界
        public bool IsOutOfBounds()
        {
            // 赛道边界：左右各留出一定空间
            float leftBoundary = 50;  // 左边界
            float rightBoundary = GameEngine.GAME_WIDTH - 50; // 右边界
            
            return X < leftBoundary || X > rightBoundary;
        }
        
        // 静态资源，避免重复创建
        private static readonly SolidBrush shadowBrush = new SolidBrush(Color.FromArgb(100, 0, 0, 0));
        private static readonly Pen carBorderPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
        private static readonly Font numberFont = new Font("Courier New", 8, FontStyle.Bold);
        private static readonly SolidBrush numberBrush = new SolidBrush(Color.White);
        private static readonly SolidBrush frontBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 0));
        private static readonly SolidBrush rearBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 0));
        private static readonly SolidBrush windowBrush = new SolidBrush(Color.FromArgb(255, 0, 255, 255));
        
        public void Render(Graphics graphics)
        {
            // 保存当前图形状态
            Matrix originalMatrix = graphics.Transform;
            
            // 设置旋转矩阵
            Matrix rotationMatrix = new Matrix();
            rotationMatrix.RotateAt(RotationAngle, new PointF(X, Y));
            graphics.Transform = rotationMatrix;
            
            // 绘制赛车阴影 - 像素风格
            graphics.FillRectangle(shadowBrush, X - Width / 2 + 1, Y - Height / 2 + 1, Width, Height);
            
            // 绘制赛车主体 - 8位机像素风格
            SolidBrush carBrush = new SolidBrush(GetCarColor());
            graphics.FillRectangle(carBrush, X - Width / 2, Y - Height / 2, Width, Height);
            
            // 绘制赛车边框 - 黑色像素边框
            graphics.DrawRectangle(carBorderPen, X - Width / 2, Y - Height / 2, Width, Height);
            
            // 绘制赛车细节 - 像素风格
            DrawPixelCarDetails(graphics);
            
            // 绘制赛车编号 - 白色像素字体
            string number = GetCarNumber();
            SizeF numberSize = graphics.MeasureString(number, numberFont);
            float numberX = X - numberSize.Width / 2;
            float numberY = Y - numberSize.Height / 2;
            graphics.DrawString(number, numberFont, numberBrush, numberX, numberY);
            
            // 恢复原始图形状态
            graphics.Transform = originalMatrix;
            
            carBrush.Dispose();
        }
        
        private void DrawPixelCarDetails(Graphics graphics)
        {
            // 绘制像素风格的车头 - 黑色前脸
            graphics.FillRectangle(frontBrush, X - Width / 2, Y - Height / 2, 2, Height);
            
            // 绘制像素风格的车尾 - 黑色后脸
            graphics.FillRectangle(rearBrush, X + Width / 2 - 2, Y - Height / 2, 2, Height);
            
            // 绘制像素风格的车窗 - 青色车窗
            graphics.FillRectangle(windowBrush, X - Width / 2 + 3, Y - Height / 2 + 3, Width - 6, Height / 3);
            
            // 绘制像素风格的车轮 - 黑色小方块
            graphics.FillRectangle(frontBrush, X - Width / 2 + 1, Y - Height / 2 + 1, 2, 2);
            graphics.FillRectangle(frontBrush, X + Width / 2 - 3, Y - Height / 2 + 1, 2, 2);
            graphics.FillRectangle(frontBrush, X - Width / 2 + 1, Y + Height / 2 - 3, 2, 2);
            graphics.FillRectangle(frontBrush, X + Width / 2 - 3, Y + Height / 2 - 3, 2, 2);
            
            // 为加油车绘制红十字标识
            if (Name.ToLower().Contains("fueltruck"))
            {
                DrawRedCross(graphics);
            }
        }
        
        private void DrawRedCross(Graphics graphics)
        {
            // 创建红色画笔
            SolidBrush redBrush = new SolidBrush(Color.FromArgb(255, 255, 0, 0));
            
            // 绘制红十字 - 垂直部分
            graphics.FillRectangle(redBrush, X - 1, Y - Height / 2 + 4, 2, Height - 8);
            
            // 绘制红十字 - 水平部分
            graphics.FillRectangle(redBrush, X - Width / 2 + 4, Y - 1, Width - 8, 2);
            
            redBrush.Dispose();
        }
        
        private Color GetCarColor()
        {
            switch (Name.ToLower())
            {
                case "ferrari": return Color.FromArgb(255, 255, 0, 0); // 红色 - 主角
                case "mclaren": return Color.FromArgb(255, 255, 165, 0); // 橙色
                case "mercedes": return Color.FromArgb(255, 0, 128, 0); // 绿色
                case "red bull": return Color.FromArgb(255, 0, 0, 255); // 蓝色
                case "williams": return Color.FromArgb(255, 255, 255, 255); // 白色
                case "enemy1": return Color.FromArgb(255, 0, 255, 255); // 青色敌方车辆
                case "enemy2": return Color.FromArgb(255, 255, 0, 255); // 紫色敌方车辆
                case "enemy3": return Color.FromArgb(255, 255, 255, 0); // 黄色敌方车辆
                case "enemy4": return Color.FromArgb(255, 0, 255, 0); // 绿色敌方车辆
                case "enemy5": return Color.FromArgb(255, 255, 165, 0); // 橙色敌方车辆
                case "enemy6": return Color.FromArgb(255, 128, 0, 128); // 紫色敌方车辆
                case "enemy7": return Color.FromArgb(255, 0, 128, 128); // 青色敌方车辆
                case "enemy8": return Color.FromArgb(255, 128, 128, 0); // 橄榄色敌方车辆
                case "fueltruck":
                case "fueltruck1":
                case "fueltruck2":
                case "fueltruck3": return Color.FromArgb(255, 255, 255, 255); // 白色 - 加油车
                default: return Color.FromArgb(255, 128, 128, 128); // 灰色
            }
        }
        
        private string GetCarNumber()
        {
            switch (Name.ToLower())
            {
                case "ferrari": return "16";
                case "mclaren": return "4";
                case "mercedes": return "44";
                case "red bull": return "1";
                case "williams": return "23";
                case "enemy1": return "01";
                case "enemy2": return "02";
                case "enemy3": return "03";
                case "enemy4": return "04";
                case "enemy5": return "05";
                case "enemy6": return "06";
                case "enemy7": return "07";
                case "enemy8": return "08";
                case "fueltruck":
                case "fueltruck1":
                case "fueltruck2":
                case "fueltruck3": return "FU"; // 加油车标识
                default: return "99";
            }
        }
        
        public Rectangle GetBounds()
        {
            return new Rectangle((int)X, (int)Y, Width, Height);
        }
        
        public void Reset(float x, float y)
        {
            X = x;
            Y = y;
            Speed = 0;
            Direction = 0;
            IsAccelerating = false;
            IsBraking = false;
            IsTurningLeft = false;
            IsTurningRight = false;
        }
    }
} 